package com.ruoyi.light.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.domain.light.LitConfigPush;
import com.ruoyi.common.mybatisplus.QueryWrTool;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysRoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.light.mapper.LitConfigPushMapper;
import com.ruoyi.light.service.ILitConfigPushService;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 安灯配置-异常推送 服务层实现
 *
 * @author ruoyi
 * @date 2022-02-15
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class LitConfigPushServiceImpl extends ServiceImpl<LitConfigPushMapper, LitConfigPush> implements ILitConfigPushService {

    private final ISysRoleService roleService;

    @Override
    public Page<LitConfigPush> getPage(Page<LitConfigPush> page, LitConfigPush litConfigPush) {
        Page<LitConfigPush> litConfigPushPage = this.page(page, QueryWrTool.queryWrapperByBaseEntity(litConfigPush));
        List<LitConfigPush> litConfigPushList = litConfigPushPage.getRecords();
        Set<Long> roleIdSet = new HashSet<>();
        for (LitConfigPush configPush : litConfigPushList) {
            List<Long> roleIdDiff1 = configPush.getRoleIdDiff1();
            List<Long> roleIdDiff2 = configPush.getRoleIdDiff2();
            List<Long> roleIdDiff3 = configPush.getRoleIdDiff3();
            if (roleIdDiff1 != null && roleIdDiff1.size() > 0) {
                roleIdSet.addAll(roleIdDiff1);
            }
            if (roleIdDiff2 != null && roleIdDiff2.size() > 0) {
                roleIdSet.addAll(roleIdDiff2);
            }
            if (roleIdDiff3 != null && roleIdDiff3.size() > 0) {
                roleIdSet.addAll(roleIdDiff3);
            }
        }
        if (roleIdSet.size()>0) {
            List<SysRole> roleList = roleService.list(Wrappers.lambdaQuery(SysRole.class).in(SysRole::getRoleId, roleIdSet));
            Map<Long, String> roleMap = roleList.stream().collect(Collectors.toMap(SysRole::getRoleId, SysRole::getRoleName));
            for (LitConfigPush configPush : litConfigPushList) {
                List<Long> roleIdDiff1 = configPush.getRoleIdDiff1();
                if (roleIdDiff1 != null && roleIdDiff1.size() > 0) {
                    List<String> roleNameList1 = new ArrayList<>();
                    for (Long rid : roleIdDiff1) {
                        if (roleMap.containsKey(rid)) {
                            roleNameList1.add(roleMap.get(rid));
                        }
                    }
                    configPush.setRoleNameDiff1(StringUtils.join(roleNameList1, ", "));
                }

                List<Long> roleIdDiff2 = configPush.getRoleIdDiff2();
                if (roleIdDiff2 != null && roleIdDiff2.size() > 0) {
                    List<String> roleNameList2 = new ArrayList<>();
                    for (Long rid : roleIdDiff2) {
                        if (roleMap.containsKey(rid)) {
                            roleNameList2.add(roleMap.get(rid));
                        }
                    }
                    configPush.setRoleNameDiff2(StringUtils.join(roleNameList2, ", "));
                }


                List<Long> roleIdDiff3 = configPush.getRoleIdDiff3();
                if (roleIdDiff3 != null && roleIdDiff3.size() > 0) {
                    List<String> roleNameList3 = new ArrayList<>();
                    for (Long rid : roleIdDiff3) {
                        if (roleMap.containsKey(rid)) {
                            roleNameList3.add(roleMap.get(rid));
                        }
                    }
                    configPush.setRoleNameDiff3(StringUtils.join(roleNameList3, ", "));
                }
            }
        }

        return litConfigPushPage;
    }



}